﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PMS_Service.CODE
{
    public static class SplitWord
    {
        static int MAX_TOTAL_WORDS_IN_A_COMPOUNDWORD = 5;
        private static WordDict wordDiction = new WordDict();
        public static String splitKeywords(String sText)
        {
            List<CompoundWord> list = wordDiction.CompoundDict();
            StringBuilder sOutText = new StringBuilder();
            sText += " ";
            sText = sText.Replace("\\t", " ");
            sText = sText.Replace("\t", " ");
            sText = sText.Replace("\\r", " ");
            sText = sText.Replace("\r", " ");
            sText = sText.Replace(")", " ) ");
            sText = sText.Replace("(", " ( ");
            sText = sText.Replace(". ", " .\n ");
            sText = sText.Replace("? ", " ?\n ");
            sText = sText.Replace(": ", " :\n ");
            sText = sText.Replace("! ", " !\n ");
            sText = sText.Replace(", ", " , ");
            sText = sText.Replace("; ", " ; ");
            sText = sText.Replace("\"", "");
            sText = sText.Replace("' ", "");
            sText = sText.Replace(" ", " ");

            sText = sText.Replace("\\n", "\n");
            String[] sParagraphArr = sText.Split('\n');
            foreach (String sParaItem in sParagraphArr)
            {
                string[] currWord = sParaItem.Split(' '); int i = 0;
                while (i < currWord.Length - 1)
                {
                    if (currWord[i].Length <= 0)
                    {
                        i++;
                        continue;
                    }
                    bool isAppened = false;
                    int t = MAX_TOTAL_WORDS_IN_A_COMPOUNDWORD - 1;
                    char ch = currWord[i][0]; //Ký tự đầu của word
                    if (".,;?:!".IndexOf(ch) >= 0 || !Char.IsLetter(ch))
                    {
                        t = 0;
                    }
                    else
                    {
                        while (t > 0)
                        {
                            StringBuilder strTemp = new StringBuilder();
                            strTemp.Append(currWord[i]);
                            for (int j = i + 1; j < i + 1 + t && j < currWord.Length; j++)
                            {
                                strTemp.Append("_" + currWord[j]);
                            }
                            String tmpWord = strTemp.ToString().ToLower().Trim();
                            if (wordDiction.CheckContainCompoundWord(tmpWord, list))
                            {
                                sOutText.Append(strTemp + " ");
                                isAppened = true;	//đánh dấu là đã thêm vào chuỗi output
                                break;
                            }
                            else
                            {
                                //Nếu ko phải là 1 từ ghép -> xét đến tổ hợp ngắn
                                //hơn bằng cách giảm biến đếm đi 1.
                                t--;
                            }
                        }
                    }
                    if (t == 0)
                    {
                        if (!isAppened)
                        {
                            sOutText.Append(currWord[i] + " ");
                        }
                    }
                    i += t + 1;
                }
                if (i < currWord.Length)
                {
                    sOutText.Append(currWord[i] + " ");
                }
                sOutText.Append("\n");
            }
            return sOutText.ToString();
        }


        /*    */

        public static String splitNameWords(String sText)
        {
            StringBuilder sOutText = new StringBuilder();
            List<String> vNameList = new List<String>();
            String[] sParagraphArr = sText.Split('\n');
            foreach (String sParaItem in sParagraphArr)
            {
                string[] strWordArr = sParaItem.Split(' ');
                bool isFirstPositionOfParagraph = true;
                StringBuilder aName = new StringBuilder();
                for (int i = 0; i < strWordArr.Length; i++)
                {
                    String currWord = strWordArr[i];

                    bool hasName = false;
                    if (isFirstPositionOfParagraph)
                    {
                        if (!isName(currWord))
                        {
                            isFirstPositionOfParagraph = false;
                        }
                        else
                        {
                            if (strWordArr[i + 1].Length > 0 && char.IsUpper(strWordArr[i + 1][0]))
                            {
                                aName.Append(currWord + " ");
                                hasName = true;
                                isFirstPositionOfParagraph = false;
                            }
                        }
                    }
                    else
                    {
                        if (isName(currWord))
                        {
                            aName.Append(currWord + " ");
                            hasName = true;
                        }
                        if (!hasName || i == strWordArr.Length - 1)
                        {
                            if (aName.Length > 0)
                            {
                                if (aName.ToString().Trim().IndexOf(' ') > 0)
                                {
                                    vNameList.Add(aName.ToString().Trim());
                                }


                                aName.Remove(0, aName.Length);
                            }
                        }
                    }
                }
            }
            //Xét ngược lại các trường hợp danh từ đứng ở đầu câu.
            foreach (String sParaItem in sParagraphArr)
            {
                string temp;
                temp = " " + sParaItem + " ";
                foreach (String aName in vNameList)
                {
                    String realName = aName.Replace(' ', '_');
                    temp = temp.Replace(" " + aName + " ", " " + realName + " ");
                }
                sOutText.Append(temp.Trim() + "\n");
            }
            return sOutText.ToString();
        }

        private static bool isName(string currWord)
        {
            if (currWord.Length > 0 && char.IsUpper(currWord[0]))
            {
                if (currWord.IndexOf('_') < 0)
                {
                    if (wordDiction.CheckContainPrefixWord(currWord.ToLower()))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        }


        public static string RemoveStopWord(string sText)
        {
            List<StopWord> listStop = wordDiction.StopDict();
            List<CompoundWord> listCompound = wordDiction.CompoundDict();
            StringBuilder sOutText = new StringBuilder();
            String[] sParagraphArr = sText.Split('\n');
            foreach (String sParaItem in sParagraphArr)
            {
                String[] sWordArr = sParaItem.Split(' ');
                foreach (string currWord in sWordArr)
                {
                    if (currWord.Length > 0 && wordDiction.CheckContainStopWord(currWord.Trim().ToLower(), listStop))
                    {
                        sText = sText.Replace(" " + currWord + " ", " ");

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(currWord))
                        {
                            string word = currWord.Trim();
                            word = word.Replace(".", "");
                            word = word.Replace("?", "");
                            word = word.Replace(":", "");
                            word = word.Replace("!", "");
                            word = word.Replace(",", "");
                            word = word.Replace(";", "");
                            word = word.Replace("\"", "");
                            word = word.Replace("'", "");
                            word = word.Replace(" ", "");

                            sOutText.Append(word + " ");


                        }
                    }
                }
            }
            return sOutText.ToString();
        }


    }
}
